package com.bluegalaxy.nhb.sntimer.timer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.nhb.service.AnalyseSqlService;
import com.bluegalaxy.nhb.snservice.common.utils.SpringContextUtil;
import com.bluegalaxy.nhb.snservice.support.cron.SNCron;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author:
 * @date: 2020/8/7
 * @time: 14:03
 * @description: To change this template use File | Settings | File Templates.
 */
public class SqlInfoTimer extends SNCron {

    private Logger logger = LoggerFactory.getLogger(SqlInfoTimer.class);

    public SqlInfoTimer(String cronName, long timeout, boolean isExit) {
        super(cronName, timeout, isExit);
    }

    @Override
    public void cronThreadRun() throws Exception {
        logger.info("定时任务开始执行");
        this.doJob();
        logger.info("定时任务执行完成");

    }

    private void doJob(){
//        SqlInfo sqlInfo = XxSqlJoin.start()
//                .insertInto("user").values("('3', 'lisi')")
//                .text(false, ";")
//                .end();
//        String sql = sqlInfo.getSql();
//        Object[] arr = sqlInfo.getParamsArr();
//
//        logger.info("insert"+sql);
//
//        SqlInfo querySql = XxSqlQueryJoin.start()
//                .text("1=1")
//                .andEqual("name","1234")
//                .end();
//        logger.info("querySql"+querySql.getSql());
        // 断言并输出sql信息
//        assertEquals("INSERT INTO user VALUES ('3', 'lisi')", sql);
        int data[] = {6, 7};
        List<String> list = new ArrayList<>();
        list.add("55");
        list.add("66");

        /*************************userAttributes***********************************/
        JSONArray jSONArray = new JSONArray();
        JSONObject jb = new JSONObject();
        jb.put("attributeName", "vip_level");
        jb.put("valueExpression", "equal");
        jb.put("value1Str", 1);
        jb.put("relation", "and");
        jSONArray.add(jb);

        JSONObject j1 = new JSONObject();
        j1.put("attributeName", "time");
        j1.put("valueExpression", "between");
        j1.put("value1Str", 1);
        j1.put("value2Str", 2);
        j1.put("relation", "and");
//        jSONArray.add(j1);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userAttributes",jSONArray);

        /*************************userActionOrders***********************************/
        JSONArray orderArray = new JSONArray();
        JSONObject order1 = new JSONObject();
        String range[] = {"2021-07-05","2021-07-28"};
        order1.put("dataRange",range);
        JSONArray eventItems = new JSONArray();
        JSONObject event1 = new JSONObject();
        event1.put("eventType","MPShow");
        event1.put("eventItemCode","lib");
        JSONObject eventItem1 = new JSONObject();
        eventItem1.put("eventItem",event1);
        eventItem1.put("eventItemExpression","not in");
        String values[] = {"AAA","BBB"};
        eventItem1.put("value1Str",values);
        eventItem1.put("value2Str","");
        eventItems.add(eventItem1);

        JSONObject event2 = new JSONObject();
        event2.put("eventType","pageview");
        event2.put("eventItemCode","type");
        JSONObject eventItem2 = new JSONObject();
        eventItem2.put("eventItem",event2);
        eventItem2.put("eventItemExpression","has value");
        eventItem2.put("value1Str","IOS");
        eventItem2.put("value2Str","");
        eventItems.add(eventItem2);
//        order1.put("eventItems",eventItems);

//        orderArray.add(order1);
        jsonObject.put("nextRuleTypes",eventItems);

        /*************************userActions***********************************/
        JSONArray actionsArray = new JSONArray();
        JSONObject action1 = new JSONObject();
        String rangeA[] = {"2021-07-05","2021-07-28"};
        action1.put("dataRange",rangeA);

        JSONArray eventItemsArray = new JSONArray();
        JSONObject eventA1 = new JSONObject();
        eventA1.put("eventType","MPShow");
        eventA1.put("eventItemCode","lib");
        JSONObject eventItemA1 = new JSONObject();
        eventItemA1.put("eventItem",event1);
        eventItemA1.put("eventItemExpression","not in");
        String valuesA[] = {"AAA","BBB"};
        eventItemA1.put("value1Str",valuesA);
        eventItemA1.put("value2Str","");
        eventItemsArray.add(eventItemA1);
        action1.put("eventItems",eventItemsArray);

        action1.put("doThings","do");

        JSONObject typeObj = new JSONObject();
        typeObj.put("eventType","MPShow");
        action1.put("eventType",typeObj);

        String methods[] = {"lib","sum"};
        action1.put("eventMethod",methods);

        action1.put("eventItemExpression","gatter");
        action1.put("value1Str","100000");
        actionsArray.add(action1);
//        jsonObject.put("userActions",actionsArray);


        jsonObject.put("globalStr","且");
        jsonObject.put("attributeStr","且");
        jsonObject.put("actionStr","且");
        jsonObject.put("orderStr","且");
        logger.info("param:"+jsonObject.toJSONString());

//        AssembleService assembleService =  SpringContextUtil.getBean("AssembleService");
//        String aSql = assembleService.assembleSql(jsonObject,00);
        AnalyseSqlService sqlService =  SpringContextUtil.getBean("AnalyseSqlService");
        String anaSql = sqlService.analyseSql(jsonObject,"keep","nextRuleTypes");
//        String aSql = AssembleSql.assembleSql(jsonObject,00);
        logger.info("anaSql:"+anaSql);




    }


}
